package com.ruoyi.quartz.task;



import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Set;

@Component("deleteOptimalProductTask")
public class DeleteOptimalProductTask  {

    private static final String REDIS_KEY_PREFIX = "PRODUCT::OVER::ID::*";
    private final ApplicationContext applicationContext;

    public DeleteOptimalProductTask(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }
    public void delete(){
        try {
            StringRedisTemplate stringRedisTemplate = applicationContext.getBean(StringRedisTemplate.class);
            Set<String> keys = stringRedisTemplate.keys(REDIS_KEY_PREFIX);
            if (keys != null && !keys.isEmpty()) {
                stringRedisTemplate.delete(keys);
                System.out.println("已删除以下 Redis Key：" + keys);
            }else {
                System.out.println("没有找到需要删除的 Redis Key");
            }
        }catch (Exception e) {
            System.err.println("任务执行失败：" + e.getMessage());
            e.printStackTrace();
            throw e;
        }
    }
}
